let requestUrl;
let authArray = ["不可认证", "可以认证"];

function addHandler() {
	requestUrl = "/chongwu_skill_category/insert";
	
	$("#skill_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#skill_list").datagrid("getSelected");
	
	if(rowData) {
		requestUrl = "/chongwu_skill_category/updateById";
		
		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#type").combobox("setValue", rowData.type);
		$("#score").numberspinner("setValue", rowData.score);
		$("#verifiable").combobox("setValue", rowData.verifiable);

		$("#skill_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#skill_list").datagrid("getSelected");
	
	if(rowData) {
		requestUrl = "/chongwu_skill_category/updateImageById";
		
		$("#categoryId").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#name_").textbox({
		width: 120,
		prompt: "请输入关键字",
	});
	
	$("#type_").combobox({
		url: "/chongwu_skill_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		prompt: "-技能类型-",
		panelHeight: "auto"
	});
	
	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		$("#skill_list").datagrid("reload", {
			name: $("#name_").textbox("getValue"),
			type: $("#type_").combobox("getValue")
		});
	});
	
	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 150,
		required: true
	});

	/**
	 * 技能类型
	 */
	$("#type").combobox({
		url: "/chongwu_skill_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto"
	});

	/**
	 * 评分
	 */
	$("#score").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	/**
	 * 是否可认证
	 */
	fetchSelect("#verifiable", authArray, {
		width: 150,
		required: true
	});

	/**
	 * 技能说明
	 */
	$("#note").textbox({
		width: 375,
    	height: 100,
    	required: true,
		multiline: true
	});
	
	$("#skill_dialog").dialog({
		title: "宠物技能",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#skill_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#skill_dialog").dialog('close');
						$("#skill_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#skill_form").form("clear");
				$("#skill_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#skill_list", "/chongwu_skill_category/upload");

	fetchUploadDialog("/chongwu_skill_category/updateImageById", function () {
		$("#skill_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#skill_list").datagrid({
		url: "/chongwu_skill_category/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", hidden: true},
			{field: "name", title: "技能名称", align: "center", width: 100},
			{field: "image", title: "图片", align: "center", width: 50
				, formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "score", title: "技能评分", align: "center", width: 50},
			{field: "type", title: "技能类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chongwu_skill_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#skill_type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='skill_type_" + rowIndex + "'></div>";
				}
			},
			{field: "verifiable", title: "是否可认证", align: "center", width: 100
				, formatter: function(value) {
					return "<div>" + authArray[value] + "</div>";
				}
			},
			{field: "note", title: "技能说明", width: 500
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

});